<template>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px">
      <div class="tip">数据分发配置</div>
      <el-form-item label="id" prop="id" hidden>
        <el-input v-model="dataForm.id" placeholder="id" hidden ></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="ftp" prop="ftpId">
            <el-select
              v-model.trim="dataForm.ftpId"
              filterable
              placeholder="请选择FTP"
              style="width:100%"
            >
              <el-option
                v-for="ftp in ftpOptions"
                :key="ftp.ftpId"
                :label="ftp.ftpAddress"
                :value=ftp.ftpId
              ></el-option>
            </el-select>

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资料名称" prop="ddataId">
            <el-select
              v-model.trim="dataForm.playBillId"
              filterable
              @change="getTable($event)"
              placeholder="请选择资料"
              style="width:100%"
            >
              <el-option
                v-for="dataClass in dataClassIdOptions"
                :key="dataClass.id"
                :label="dataClass.dataName"
                :value="dataClass.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="源目录" prop="sourceDirectory">
            <el-input v-model="dataForm.sourceDirectory" placeholder="源目录"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="正则匹配" prop="sourceFileName">
            <el-input v-model="dataForm.sourceFileName" placeholder="源文件名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="目标目录" prop="targetDirectory">
            <el-input v-model="dataForm.targetDirectory" placeholder="目标目录"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="重命名" prop="targetFileName">
            <el-input v-model="dataForm.targetFileName" placeholder="目标文件名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="历史数据" prop="isHistory">
       <!--     <el-radio-group v-model="dataForm.isHistory">
              <el-radio  :label=0>不分发</el-radio>
              <el-radio  :label=1>分发历史</el-radio>
              <el-radio  :label=2>仅分发历史</el-radio>
            </el-radio-group>-->
            <el-select
              v-model.trim="dataForm.isHistory"
              filterable
              style="width:100%"
            >
              <el-option
                v-for="dataClass in isHistoryOptions"
                :key="dataClass.value"
                :label="dataClass.name"
                :value="dataClass.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="定时|实时" prop="isRealTime">
            <el-radio-group v-model="dataForm.isRealTime">
              <el-radio  :label=0>实时</el-radio>
              <el-radio  :label=1>定时</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="应发文件数" prop="minFileNum">
            <el-input v-model="dataForm.minFileNum" placeholder="应发文件数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否DMZ" prop="isDmz">
            <el-radio-group v-model="dataForm.isDmz">
              <el-radio  :label=0>否</el-radio>
              <el-radio  :label=1>是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div v-if="1==dataForm.isDmz">
            <el-form-item label="dmz路径" prop="dmzPath">
              <el-input v-model="dataForm.dmzPath" placeholder="dmz路径"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <div v-if="1==dataForm.isHistory||2==dataForm.isHistory">
        <div class="tip">历史数据配置</div>
        <el-row>
          <el-col :span="12">

            <el-form-item label="历史资料开始时间" prop="historyStartTime" >
              <el-date-picker
                v-model="dataForm.dispenseHistory.historyStartTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="历史资料结束时间" prop="historyEndTime">
              <el-date-picker
                v-model="dataForm.dispenseHistory.historyEndTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <div v-if="1==dataForm.isRealTime">
        <div class="tip">定时配置</div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="cron表达式" prop="jobCron">
              <el-input v-model="dataForm.dispenseTiming.jobCron" placeholder="cron表达式"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名" prop="tableName" v-if="dataForm.dispenseTiming.collectType==0">
              <el-input v-model="dataForm.dispenseTiming.tableName" placeholder="表名"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收集类型" prop="collectType">
              <el-radio-group v-model="dataForm.dispenseTiming.collectType">
                <el-radio  :label=0>数据库</el-radio>
                <el-radio  :label=1>本地盘</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行状态" prop="triggerStatus">
              <el-radio-group v-model="dataForm.dispenseTiming.triggerStatus">
                <el-radio  :label=0>停止</el-radio>
                <el-radio  :label=1>启动</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </div>




    </el-form>
</template>
<style>
  .tip {
    padding: 8px 16px;
    background-color: #ecf8ff;
    border-radius: 4px;
    border-left: 5px solid #50bfff;
    margin: 20px 0;
  }
</style>
<script>
  import {getObj, addObj, putObj,findAllFtp} from '@/api/job/dispenseConfig'
  import {findAllPlayBill} from '@/api/job/playBill'
  export default {
    created() {
    },
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataClassIdOptions:[],
        ftpOptions:[],
        isHistoryOptions:[{
          value: 0,
          name: '不分发历史'
        },{
          value: 1,
          name: '分发历史'
        },{
          value: 2,
          name: '仅分发历史'
        },],
        dataForm: {
          type: '',
          id: '',
          ftpId: '',
          ddataId:'',
          playBillId:'',
          isHistory: 0,
          dispenseTiming:{
            executorBlockStrategy: '',
            executorFailRetryCount: '',
            executorHandler: '',
            executorParam: '',
            executorRouteStrategy: '',
            executorTimeout: '',
            isAlarm: '',
            jobCron: '',
            jobDesc: '',
            jobGroup: '',
            retryInterval: '',
            taskName: '',
            triggerLastTime: '',
            triggerNextTime: '',
            triggerStatus: '',
            tableName:'',
            collectType: 0
          },
          dispenseHistory:{
            historyStartTime:'',
            historyEndTime:'',
          },
          sourceDirectory: '',
          targetDirectory: '',
          sourceFileName: '',
          targetFileName: '',
          isRealTime:0,
          dataName: '',
          isDmz:0,
          dmzPath:'',
          minFileNum: 0,
        },
        dataRule: {

        }
      }
    },
    methods: {
      init(id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (id) {
            getObj(id).then(response => {
              this.dataForm = response.data.data;
              if(null==this.dataForm.dispenseHistory){
                this.dataForm.dispenseHistory={
                    historyStartTime:'',
                    historyEndTime:'',
                }
              }
              if(null==this.dataForm.dispenseTiming){
                this.dataForm.dispenseTiming={
                  executorBlockStrategy: '',
                  executorFailRetryCount: '',
                  executorHandler: '',
                  executorParam: '',
                  executorRouteStrategy: '',
                  executorTimeout: '',
                  isAlarm: '',
                  jobCron: '',
                  jobDesc: '',
                  jobGroup: '',
                  retryInterval: '',
                  taskName: '',
                  triggerLastTime: '',
                  triggerNextTime: '',
                  triggerStatus: '',
                  tableName:'',
                  collectType: 0,
                }
              }
            })
          }
          this.selectByDataId()
          this.selectAllFtp()
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                 this.disVisible()
                this.refreshDispenseDataList(data.data.data.id)
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.disVisible()
                this.refreshDispenseDataList(data.data.data.id)
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      refreshDispenseDataList (id) {
        this.$emit('refreshDataList',id)
      },
      disVisible () {
        this.$emit('disVisible')
      },
      //重置表单
      closeDialog() {
        this.$refs["dataForm"].resetFields()
      },
      selectByDataId() {
        findAllPlayBill().then(response => {
          this.dataClassIdOptions = response.data.data;
        });
      },
      selectAllFtp() {
        findAllFtp().then(response => {
          this.ftpOptions = response.data.data;
         /* for (let i = 0; i < this.ftpOptions.length; i++) {
            this.ftpOptions[i].ftpId=parseInt(this.ftpOptions[i].ftpId)
          }*/
        });
      },
      getTable(ddataId){
        for (let i = 0; i < this.dataClassIdOptions.length; i++) {
          if(this.dataClassIdOptions[i].id==ddataId){
             this.dataForm.dataName=this.dataClassIdOptions[i].dataName;
             this.dataForm.ddataId=this.dataClassIdOptions[i].ddataId;
             this.dataForm.dispenseTiming.tableName=this.dataClassIdOptions[i].tableName;
             console.log( this.dataForm.dataName)
           }
         }

      }
    }
  }
</script>

